package com.neusoft.databus.agent.pool.ptrest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import com.neusoft.databus.common.connection.AbstractConnection;
import com.neusoft.databus.common.connection.JhportstatusConnection;
import com.neusoft.databus.common.pool.Connector;

import lombok.extern.slf4j.Slf4j;

/**
 * description
 *
 * @author sunchf
 * @date 2019年4月2日 上午9:27:07
 */
@Slf4j
@Component
public class JhrestConnector implements Connector<RestTemplate> {

    @Autowired
    private JhrestPool pool;

    @Override
    public RestTemplate getConn(AbstractConnection connection) throws Exception {
        final RestTemplate cli = this.pool.borrowObject((JhportstatusConnection) connection);
        return cli;
    }

    @Override
    public RestTemplate getConn(AbstractConnection connection, int timeout) throws Exception {
        final RestTemplate cli = this.pool.borrowObject((JhportstatusConnection) connection, timeout * 1000);
        return cli;
    }

    @Override
    public void returnConn(AbstractConnection connection, RestTemplate conn) {
        this.pool.returnObject((JhportstatusConnection) connection, conn);
    }

    @Override
    public void invalidateConn(AbstractConnection connection, RestTemplate conn) {
        try {
            this.pool.invalidateObject((JhportstatusConnection) connection, conn);
        } catch (final Exception e) {
            JhrestConnector.log.error(e.getMessage(), e);
        }
    }

    @Override
    public void removeAllConns(AbstractConnection connection) {
        try {
            this.pool.clear();
        } catch (final Exception e) {
            JhrestConnector.log.error(e.getMessage(), e);
        }
    }

}
